package pers.cz.codegen.describe;

import pers.cz.jefconfig.log.LogUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @program: Jef-code-generator
 * @description: java内部类
 * @author: Cheng Zhi
 * @create: 2023-08-13 16:39
 **/
public class JavaInnerClazz extends JavaUnit {

    public JavaInnerClazz(String name, UnitType anEnum) {
        super(name, anEnum);
    }

    public JavaInnerClazz(String name) {
        super(name);
    }
    @Override
    public String toCode() {

        StringBuilder sb = new StringBuilder();

        for (String annotation : this.annotations) {
            sb.append(TAB_SYMBOL);
            sb.append(annotation);
            sb.append(LINE_SYMBOL);
        }
        sb.append(TAB_SYMBOL);
        sb.append(getModifer());
        sb.append(classType);
        sb.append(SPACE_SYMBOL);
        sb.append(getName());

        if (isChild) {
            sb.append(SPACE_SYMBOL);
            sb.append("extends");
            sb.append(SPACE_SYMBOL);
            sb.append(this.parentName);
        }

        if (isImpl) {
            sb.append(SPACE_SYMBOL);
            sb.append("implements");
            sb.append(SPACE_SYMBOL);
            int countInf = 0;
            int infSize = interfaces.size();
            for (String interfaceName : interfaces) {
                countInf ++;
                sb.append(interfaceName);
                if (countInf != infSize) {
                    sb.append(",");
                }
            }
        }
        sb.append(SPACE_SYMBOL);
        sb.append(START_SYMBOL);

        int enumConut = 0;
        for (JavaElement enumEnty : enums) {
            sb.append(TAB_SYMBOL);
            sb.append(enumEnty.toCode());
            enumConut ++;
            if (enumConut != enums.size()) {
                sb.append(",");
            } else {
                sb.append(SEMICOLON);
            }
            sb.append(LINE_SYMBOL);
        }

        for (JavaElement filed : fields) {
            sb.append(filed.toCode());
        }

        for (JavaElement method : methods) {
            sb.append(TAB_SYMBOL);
            sb.append(method.toCode());
        }

        for (JavaElement innerClazz : innerClazzs) {
            sb.append(TAB_SYMBOL);
            sb.append(innerClazz);
        }

        sb.append(LINE_SYMBOL);
        sb.append(TAB_SYMBOL);
        sb.append(END_SYMBOL);

        return sb.toString();
    }

}
